package org.waddy.framework.pub.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import org.springframework.jdbc.core.RowCallbackHandler;
import org.waddy.framework.app.exception.AppException;
import org.waddy.framework.app.exception.SysException;
import org.waddy.framework.app.model.dao.Sql;
import org.waddy.framework.app.model.dao.impl.DAOImpl;
import org.waddy.framework.pub.dao.ISysDateDAO;




public class SysDateDAOImpl extends DAOImpl implements ISysDateDAO {

	public Date getCurrentOracleSysDate() throws SysException, AppException {
		Date date = null;
		Sql sql = new Sql("select sysdate from dual");
		sql.log(this.getClass());
		List<Object> params = new ArrayList<Object>();
		final List data = new ArrayList();
		try {
			jdbcTemplate.query(sql.getSql(), params.toArray(),
					new RowCallbackHandler() {
						public void processRow(ResultSet rs)
								throws SQLException {
							Object obj = rs.getTimestamp(1);
							data.add(obj);
						}
					});

			if (data.size() > 0) {
				date = (Date) data.get(0);
			}

		} catch (Exception e) {
			throw new SysException("20001", e);
		}
		return date;
	}
	
	public Date getCurrentMysqlSysDate() throws SysException, AppException {
		Date date = null;
		Sql sql = new Sql("select now()");
		sql.log(this.getClass());
		List<Object> params = new ArrayList<Object>();
		final List data = new ArrayList();
		try {
			jdbcTemplate.query(sql.getSql(), params.toArray(),
					new RowCallbackHandler() {
				public void processRow(ResultSet rs)
						throws SQLException {
					Object obj = rs.getTimestamp(1);
					data.add(obj);
				}
			});
			
			if (data.size() > 0) {
				date = (Date) data.get(0);
			}
			
		} catch (Exception e) {
			throw new SysException("20001", e);
		}
		return date;
	}

}
